#include "operationbase.h"
#include <QList>
#include "statusactor.h"
#include "deviceactor.h"
#include "stringproperty.h"

namespace VrDataCore
{
	OperationBase::OperationBase():_statusName(""),_statusActor(NULL)
	{
		setClassName("OperationBase");
	}

	OperationBase::~OperationBase()
	{}

	void OperationBase::setBindingStatus(QString statusname)
	{
		_statusName = statusname;
	}

	QString OperationBase::getBindingStatus()
	{
		return _statusName;
	}

	void OperationBase::setBindingStatusActor(StatusBase* statusactor)
	{
		_statusActor = statusactor;
	}

	StatusBase* OperationBase::getBindingStatusActor()
	{
		return _statusActor;
	}

	void OperationBase::setOperationObject(DrawableActor* obj)
	{
		_objectActor = obj;
		if(_statusName!="")
		{
			DeviceActor* dev = dynamic_cast<DeviceActor*>(obj);
			if(dev!=NULL)
			{
				QList<osg::ref_ptr<CoreBase::ActorBase>> list = dev->getAllStatusActorNoDefault();
				for(int i=0;i<list.size();++i)
				{
					if(list.at(i)->getName() == _statusName)
					{
						setBindingStatusActor(dynamic_cast<StatusActor*>(list.at(i).get()));
					}
				}
			}
		}
	}

	DrawableActor* OperationBase::getOperationObject()
	{
		return _objectActor;
	}

	void OperationBase::acceptEvent()
	{
		if(_statusActor!=NULL)
		{
			dynamic_cast<DeviceActor*>(_objectActor)->setValidStatusActor(dynamic_cast<StatusActor*>(_statusActor));
		}
	}

	bool OperationBase::isAutoEvent()
	{
		return false;
	}

	void OperationBase::buildPropertyMap()
	{
		addProperty(new StringProperty<OperationBase>(XML_ELEMENT_DATATYPE_STRING,
			"status name","",this,&OperationBase::setBindingStatus,
			&OperationBase::getBindingStatus,"",""));
	}

	void OperationBase::onMapLoadEnd(CoreBase::Map*)
	{}
}